core.form.Button = function(aParams){
	this.init(aParams);
}

core.extendClass('core.form.Button', 'core.form.UIControl');

core.form.Button.prototype.setDisabled = function(aDisabled){
	var doDisable = this.disabled!=aDisabled;
	this.disabled = aDisabled;
	if(!doDisable)
		return;
	if(aDisabled){
		this.dDiv = core.newDiv(this.div, 'disabled');
		$(this.dDiv).width(this.minWidth);
		$(this.dDiv).mousedown(function(){
			return false;
		})
		$(this.dDiv).height(this.minHeight);
	}else{
		if(this.dDiv)
			$(this.dDiv).remove();
		this.dDiv = null;
	}
	this.refresh();
}

core.form.Button.prototype.init = function(aParams){
	this.xType = 'button';
	this.superCall('init', arguments, aParams);
	this.text = aParams.text;
	this.icon = aParams.icon;
	this.minWidth = this.text?BUTTON_WIDTH:BUTTON_WIDTH_ICON;
	this.minHeight = BUTTON_HEIGHT;
	
	var d = document.createElement('div');
	$(d).addClass('button');
	
	$(d).width(this.minWidth-2);
	$(d).height(this.minHeight-2);
	
	this.div.appendChild(d);
	this.buttonDiv = d;
	this.captionDiv = document.createElement('div');
	this.buttonDiv.appendChild(this.captionDiv);
	
	var instance = this;
	var clEvent = function(e){
		if(instance.disabled)
			return false;
		return instance.fire('press', e);
	}
	
	$(this.buttonDiv).mousedown(function(e){
		if(instance.disabled)
			return false;
		$(d).addClass('button_pressed');
		instance.fire('focus', e);
		return false;
	});
	
	$(this.buttonDiv).mouseup(function(e){
		if(instance.disabled)
			return false;
		$(d).removeClass('button_pressed');
		return false;
	});
	
	$(this.buttonDiv).mouseout(function(e){
		if(instance.disabled)
			return false;
		$(d).removeClass('button_pressed');
		return false;
	});
	$(this.buttonDiv).click(clEvent);
}

core.form.Button.prototype.resize = function(aX, aY, aWidth, aHeight){
	this.superCall('resize', arguments, aX, aY, aWidth, aHeight);
	$(this.captionDiv).html(this.imagesToHTML(this.icon)+(this.params.text || ''));
	this.alignBlock(this.buttonDiv, $(this.div).innerWidth(), $(this.div).innerHeight());
	if(this.dDiv){
//		console.log('align disabled block', this.dDiv, $(this.div).innerWidth(), $(this.div).innerHeight());
		this.alignBlock(this.dDiv, $(this.div).innerWidth(), $(this.div).innerHeight());
	}
	this.alignElement(this.captionDiv, $(this.buttonDiv).innerWidth(), $(this.buttonDiv).innerHeight(), 'center', 'middle');
}

core.form.Button.prototype.doFocus = function(){
	$(this.buttonDiv).addClass('button_selected');
//	$(this.buttonDiv).focus();
	return true;
}

core.form.Button.prototype.doUnFocus = function(){
	$(this.buttonDiv).removeClass('button_selected');	
}

core.form.Button.prototype.doKeyPress = function(e){
	if(this.disabled)
		return true;
	if(e.which==32 || e.which==10 || e.which==13){
		this.fire('press', e);
		return false;
	}
	return true;
}
